package com.wyj.chat.websocket_chat.service.impl;

import com.wyj.chat.websocket_chat.dao.UserMapper;
import com.wyj.chat.websocket_chat.pojo.Friends;
import com.wyj.chat.websocket_chat.pojo.Information;
import com.wyj.chat.websocket_chat.pojo.User;
import com.wyj.chat.websocket_chat.service.FriendsService;
import com.wyj.chat.websocket_chat.service.InformationService;
import com.wyj.chat.websocket_chat.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * Create by wyj on 2018/3/13
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private FriendsService friendsService;

    @Autowired
    private InformationService informationService;

    @Override
    public User userLogin(User user) {
        User userExt = userMapper.userLogin(user);
        if(userExt != null) {
            // 获取个人信息
            Information information = informationService.getById(userExt.getId());
            // 获取好友列表
            List<Friends> friends = friendsService.getListById(userExt.getId());
            // 封装用户对象
            userExt.setInformation(information);
            userExt.setFriends(friends);
            return userExt;
        }

        return null;
    }

    @Override
    public User surePass(String name, String password) {
        User param = new User();
        param.setName(name);
        param.setPassWord(password);
        User userExt = userMapper.userLogin(param);
        return userExt;
    }

    public User getById(String id) {
        return userMapper.getById(id);
    }

    public int insertUser(User user) {
        return userMapper.insertUser(user);
    }

    public int updateUser(User user) {
        return userMapper.updateUser(user);
    }

    public int updatePassWordByName(String name,String passWord) {
        User user = new User();
        user.setName(name);
        user.setPassWord(passWord);
        return userMapper.updatePassWordByName(user);
    }

    public User getByName(String name) {
        return userMapper.getByName(name);
    }


}
